﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace HurlBat.XEdit2D
{
    public partial class ImageNode : Node
    {
        List<ImageData> images = new List<ImageData>();
        int currentImage = -1;

        public ImageNode()
            : base()
        {
            InitializeComponent();
        }

        public ImageNode(DiagramPanel container, string title)
            : base(container, Icons.image, title, Color.FromArgb(209, 210, 237), Color.FromArgb(66, 60, 103), Color.Black)
        {
            InitializeComponent();
            AddContextMenuItem("addToolStripMenuItem", "Add", new EventHandler(this.addToolStripMenuItem_Click));
            AddContextMenuItem("clearToolStripMenuItem", "Clear", new EventHandler(this.clearToolStripMenuItem_Click));
            this.Paint += new PaintEventHandler(ImageNode_Paint);
        }

        #region Events
        private void ImageNode_Paint(object sender, PaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.ForeColor);
            e.Graphics.DrawString(string.Format("{0} image{1}", images.Count, images.Count == 1 ? "" : "s"), 
                new Font("Arial", 10, FontStyle.Regular), b, 3, 30);
            if (currentImage > -1)
            {
                e.Graphics.DrawString(string.Format("{0}/{1}", currentImage + 1, images.Count),
                    new Font("Arial", 10, FontStyle.Regular), b, 3, this.Height - 18);
                imgThumb.Image = images[currentImage].Thumbnail;
            }
            imgThumb.Enabled = btnFirst.Enabled = btnLast.Enabled = btnNext.Enabled =
                btnPrevious.Enabled = currentImage > -1;
        }

        private void addToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Multiselect = true;
            DialogResult res = op.ShowDialog();
            if (res != DialogResult.OK) return;

            //Form.ResetProgressBar();

            for (int i = 0; i < op.FileNames.Length; i++){
                images.Add(new ImageData(op.FileNames[i]));
                //Form.UpdateProgressBar((i * 100) / op.FileNames.Length);
            }
            //Form.HideProgressBar();
            if (currentImage == -1) currentImage = 0;
            this.Invalidate();
        }

        private void clearToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            images.Clear();
            currentImage = -1;
            this.Invalidate();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (currentImage == -1) return;
            currentImage = 0;
            this.Invalidate();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currentImage <= 0) return;
            currentImage--;
            this.Invalidate();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentImage == images.Count - 1) return;
            currentImage++;
            this.Invalidate();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (currentImage == -1) return;
            currentImage = images.Count - 1;
            this.Invalidate();
        }
        #endregion

        public override bool AllowOutput()
        {
            return arrows.Count == 0;
        }
    }
}
